package com.example.block_chain.service.impl.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.block_chain.common.ResponseResult;
import com.example.block_chain.domain.dto.AdminDto;
import com.example.block_chain.domain.pojo.Admin;
import com.example.block_chain.mapper.AdminMapper;
import com.example.block_chain.service.admin.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {


    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public ResponseResult addAdmin(AdminDto dto) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(dto, admin);

        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        String currentMillis = String.valueOf(System.currentTimeMillis());
        // 将年份和毫秒数拼接成字符串
        String aNo = currentYear + currentMillis.substring(currentMillis.length() - 5);
        admin.setANo(aNo);

        String aId = dto.getAID();
        if (aId.length() < 6) {
            return ResponseResult.errorResult(500, "身份证的长度不合适");
        }
        // 将aId的后六位作为密码
        String password = aId.substring(Math.max(0, aId.length() - 6));

        System.out.println("password=" + password);
        String encodedPassword = passwordEncoder.encode(password);

        admin.setPassword(encodedPassword);

        adminMapper.insert(admin);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteAdmin(AdminDto dto) {
        Long id = dto.getId();

        adminMapper.deleteById(id);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateAdmin(AdminDto dto) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(dto, admin);

        adminMapper.updateById(admin);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult selectAdmin(AdminDto dto) {
        String facultyCode = dto.getFacultyCode();
        String schoolCode = dto.getSchoolCode();
        Integer page = dto.getPage();
        Integer pageSize = dto.getPageSize();

        QueryWrapper<Admin> queryWrapper = new QueryWrapper();

        List<Admin> admins;
        IPage<Admin> adminIPage = new Page<>(page, pageSize);
        if ((facultyCode == "" || facultyCode == null) && (schoolCode == "" || schoolCode == null)) {
        } else if (facultyCode == "" || facultyCode == null) {
            queryWrapper.eq("school_code", schoolCode);
        } else if (schoolCode == "" || schoolCode == null) {
            queryWrapper.eq("faculty_code", facultyCode);
        } else {
            queryWrapper.eq("school_code", schoolCode).eq("faculty_code", facultyCode);
        }
        admins = adminMapper.selectPage(adminIPage, queryWrapper).getRecords();

        return ResponseResult.okResult(admins);
    }

    @Override
    public ResponseResult selectAdmin(String facultyCode, String schoolCode, String authority, Integer page, Integer pageSize) {
        Page<Admin> p = new Page<>(page, pageSize);
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!facultyCode.isBlank(), Admin::getFacultyCode, facultyCode)
                .eq(!schoolCode.isBlank(), Admin::getSchoolCode, schoolCode)
                .eq(!authority.isBlank(), Admin::getAuthority, authority);
        Page<Admin> adminPage = adminMapper.selectPage(p, queryWrapper);
        return ResponseResult.okResult(adminPage);
    }
}
